package com.lpit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lpit.constant.Constant;
import com.lpit.dto.BarChartDto;
import com.lpit.exception.AdminException;
import com.lpit.pojo.Article;
import com.lpit.service.ArticleService;
import com.lpit.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【article(文章表)】的数据库操作Service实现
* @createDate 2023-06-08 11:06:00
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService{
    @Autowired
    ArticleMapper articleMapper;
    /**
     * @author yaochen
     * @apiNote 查询文章的总量。
     */
    @Override
    public Integer articlesNumber(){
        Integer articlesNumber = articleMapper.selectArticlesNumber();
        if( 0 == articlesNumber){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return articlesNumber;
    }
    /**
     * @author yaochen
     * @apiNote 查询评论的总数量
     */
    @Override
    public Integer commentsNumber(){
        Integer comments = articleMapper.selectCommentsNumber();
        if( 0 == comments){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return comments;
    }
    /**
     * @author yaochen
     * @apiNote 查询阅读量
     */
    @Override
    public Integer readingQuantity(){
        Integer readingQuantity = articleMapper.selectReadingQuantity();
        if(0 == readingQuantity){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return readingQuantity;
    }
    /**
     * @author yaochen
     * @apiNote 查询转发量
     */
    @Override
    public Integer forwardingNumber(){
        Integer forwardingNumber = articleMapper.selectForwardingNumber();
        if(0 == forwardingNumber){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return forwardingNumber;
    }
    /**
     * @author yaochen
     * @apiNote 柱状图，按时间查询文章数量。
     */
    @Override
    public List<BarChartDto> articlesNumberTime(String startDate,String endDate){
        List<BarChartDto> articles = articleMapper.selectArticlesNumberTime(startDate,endDate);
        if(null == articles || 0 == articles.size()){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return articles;
    }

    /**
     * @author yaochen
     * @apiNote 柱状图，按时间查询阅读数量。
     */
    @Override
    public List<BarChartDto> selectReadingQuantity(String startDate, String endDate) {
        List<BarChartDto> readingQuantity = articleMapper.selectReadingQuantityTime(startDate,endDate);
        if(null == readingQuantity || 0 == readingQuantity.size()){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return readingQuantity;
    }
    /**
     * @author yaochen
     * @apiNote 柱状图，按时间查询评论数量。
     */
    @Override
    public List<BarChartDto> selectCommentsNumber(String startDate, String endDate) {
        List<BarChartDto> commentsNumber = articleMapper.selectCommentsNumberTime(startDate,endDate);
        if(null == commentsNumber || 0 == commentsNumber.size()){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return commentsNumber;
    }

    /**
     * 文章置顶
     * @return
     */
    public Integer findByIdIsTop(Integer id){
        return articleMapper.findByIdIsTop(id);
    }
}




